# 引入了style终端控制字符
from comm.style import *


class Panel():

    def __init__(self) -> None:
        '''显示面板:
        位置x,y 宽高: w, h 
        标题: title, titleColor, titleBg
        内容: content, color, bgcolor
        行为: hide, show, moveto
        '''
        # 位置\大小
        self. x = 1
        self. y = 1
        self.w = 20
        self.h = 10
        # 颜色: 内容
        self.bgcolor = s_bgwhite
        self.color = s_black
        # 颜色: 标题
        self.titleColor = s_lwhite
        self.titleBg = s_lbgblack
        # 内容
        self.title = 'TITLE'
        self.content = 'content here'
        self.surface = ''

        # 状态
        self.visible = True
    
    def update(self):
        pass

    # 绘制: 标题
    def drawTitle(self):
        title = s_moveto(self.x, self.y) + self.titleBg + ' '*self.w + \
                s_moveto(self.x, self.y) + self.titleColor + self.title + s_reset
        return title

    # 绘制: 内容
    def drawContent(self):
        bg = self.bgcolor
        for i in range(1, self.h + 1):
            bg += s_moveto(self.x, self.y + i) + ' '*self.w
        bg += s_moveto(self.x, self.y+1) + self.color + self.content + s_reset
        return bg

    # 刷新/合成: 面板
    def refresh(self):
        self.update()
        if self.visible:
            self.surface =  self.drawTitle() + \
                            self.drawContent() + \
                            s_reset
        else:
            self.surface = ''

        return self.surface

    def moveto(self, x, y):
        self.x = x
        self.y = y
        self.refresh()

    def hide(self):
        self.visible = False
        self.refresh()

    def show(self):
        self.visible = True
        self.refresh()

class ItemList(Panel):
    def __init__(self, ship) -> None:       
        super().__init__()
        self.owner = ship
        self.x = 29
        self.y = 2
        self.w = 30
        self.h = 10

        self.title = '货舱'

    def update(self):
        self.content = ''
        items = []
        idx = 0
        keys = self.owner.cargoHold.keys()
        for key in list(keys):
            value = self.owner.cargoHold[key]            
            items.append({key:value})
            self.content += s_moveto(self.x, self.y+idx+1) + ('%2d ' % idx)+ s_left(key,20) + (' %4d' % value)
            idx += 1

# 状态面板
class StatePanel(Panel):
    def __init__(self, activeShip) -> None:
        super().__init__()
        self.ship = activeShip
        self.x ,self.y = 3,2
        self.w, self.h = 24, 10

        self.title = self.ship.type + ":" + self.ship.name

    def update(self):
        self.content = ''
        
        states = [  ('护  盾:',20,100),
                    ('装  甲:',80,100),
                    ('结  构:',100,100),
                    ('能  量:',self.ship.energy,100),
                    ]
        idx = 0
        for state in states:
            num = 1
            key = state[0]
            value = state[1]
            maxvalue = state[2]
            if value > maxvalue*0.3:
                color = s_lblack
            elif value <= 0:
                num = 0
                color = s_lblack
            else:
                color = s_lred
            self.content += s_moveto(self.x+1, self.y+1+ idx)  + self.color + self.bgcolor + \
                            s_left(key,8)  + \
                            s_moveto(self.x + 8, self.y+1+ idx)   + '.'* 10 + ('%5d' % value)+ \
                            s_moveto(self.x + 8, self.y+1+ idx) + color + '\u2587'* (int(value*9.9)//maxvalue + num) + \
                            s_reset
            idx += 1